const config     = require('config');
const path       = require('path');
const requireAll = require('require-all');
const flat       = require('flat');
const koaCompose = require('koa-compose');
const Router     = require('koa-router');

// load all controllers
const controllers = requireAll({
    dirname: path.join(config.root, 'app/controller'),
});
const ctls        = flat(controllers);
const mds         = [];
Object.keys(ctls).forEach((k) => {
    const prefix = `/${k.replace('.', '/').replace(/\/index$/, '')}`;
    const router = new Router();
    router.prefix(prefix);
    ctls[k](router);
    mds.push(router.routes());
});
module.exports = koaCompose(mds);
